
package Formularios;

import Clases.Cliente;
import Clases.ConexionMySql;
import java.sql.Connection;
import java.sql.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;


public class clientes extends javax.swing.JFrame {
    private DefaultTableModel busqueda;

   
    public clientes() {
        initComponents();
        llamar();
        
        
        id_cliente.setEnabled(false);
        nombre_cliente.setEnabled(false);
        ci_cliente.setEnabled(false);
        direccion_cliente.setEnabled(false);
        telefono_cliente.setEnabled(false);
        ciudad_cliente.setEnabled(false);
        ingreso_cliente.setEnabled(false);
        agregar_cliente.setEnabled(false);
        eliminar_cliente.setEnabled(false);
        actualizar_cliente.setEnabled(false);
        
    }

  
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        id_cliente = new javax.swing.JTextField();
        nombre_cliente = new javax.swing.JTextField();
        ci_cliente = new javax.swing.JTextField();
        direccion_cliente = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        telefono_cliente = new javax.swing.JTextField();
        agregar_cliente = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        ciudad_cliente = new javax.swing.JTextField();
        eliminar_cliente = new javax.swing.JButton();
        actualizar_cliente = new javax.swing.JButton();
        nuevo_cliente = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        ingreso_cliente = new com.toedter.calendar.JDateChooser();
        jLabel8 = new javax.swing.JLabel();
        atras = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        vista = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        cliente_busqueda = new javax.swing.JTextField();
        buscar_nombre = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Cliente", 1, 0));

        jLabel1.setText("ID:");

        jLabel2.setText("Nombre y Apellido:");

        jLabel4.setText("C.I.:");

        jLabel5.setText("Dirección:");

        id_cliente.setBorder(javax.swing.BorderFactory.createBevelBorder(0));
        id_cliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                id_clienteActionPerformed(evt);
            }
        });
        id_cliente.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                id_clienteKeyTyped(evt);
            }
        });

        nombre_cliente.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                nombre_clienteKeyTyped(evt);
            }
        });

        ci_cliente.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                ci_clienteKeyTyped(evt);
            }
        });

        direccion_cliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                direccion_clienteActionPerformed(evt);
            }
        });
        direccion_cliente.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                direccion_clienteKeyTyped(evt);
            }
        });

        jLabel6.setText("Telefono:");

        telefono_cliente.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                telefono_clienteKeyTyped(evt);
            }
        });

        agregar_cliente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/guardar.png"))); // NOI18N
        agregar_cliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregar_clienteActionPerformed(evt);
            }
        });

        jLabel7.setText("Ciudad:");

        ciudad_cliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ciudad_clienteActionPerformed(evt);
            }
        });
        ciudad_cliente.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                ciudad_clienteKeyTyped(evt);
            }
        });

        eliminar_cliente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/excluded.png"))); // NOI18N
        eliminar_cliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eliminar_clienteActionPerformed(evt);
            }
        });

        actualizar_cliente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/editar.png"))); // NOI18N
        actualizar_cliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                actualizar_clienteActionPerformed(evt);
            }
        });

        nuevo_cliente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/agregar.gif"))); // NOI18N
        nuevo_cliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nuevo_clienteActionPerformed(evt);
            }
        });

        jLabel9.setText("Fecha de Ingreso:");

        ingreso_cliente.setDateFormatString("yyyy-MM-dd");
        ingreso_cliente.setOpaque(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(nombre_cliente, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel1)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel7)
                                .addComponent(jLabel6)))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addComponent(jLabel4))
                        .addGap(20, 20, 20)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(ingreso_cliente, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
                            .addComponent(direccion_cliente)
                            .addComponent(id_cliente)
                            .addComponent(ci_cliente)
                            .addComponent(telefono_cliente)
                            .addComponent(ciudad_cliente))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 89, Short.MAX_VALUE)
                        .addComponent(agregar_cliente, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(eliminar_cliente, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(actualizar_cliente, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(nuevo_cliente, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(35, 35, 35))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(id_cliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(19, 19, 19)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ci_cliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(nombre_cliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(direccion_cliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(jLabel6))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(telefono_cliente)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(ciudad_cliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9)
                    .addComponent(ingreso_cliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nuevo_cliente, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(actualizar_cliente, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(eliminar_cliente, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(agregar_cliente))
                .addGap(19, 19, 19))
        );

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 30)); // NOI18N
        jLabel8.setText("Cliente");

        atras.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/close.png"))); // NOI18N
        atras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                atrasActionPerformed(evt);
            }
        });

        vista.setBackground(new java.awt.Color(204, 204, 204));
        vista.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", 1, 0));
        vista.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Identificador", "CI o RUC", "Nombre", "Direccion", "Telefono", "Ciudad", "Fecha de Ingreso"
            }
        ));
        vista.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                vistaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(vista);

        jLabel3.setText("Nombre: ");

        buscar_nombre.setText("Buscar");
        buscar_nombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscar_nombreActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(292, 292, 292)
                        .addComponent(jLabel8))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane1)
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(cliente_busqueda, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29)
                        .addComponent(buscar_nombre)
                        .addGap(48, 48, 48)
                        .addComponent(atras, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(26, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cliente_busqueda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(buscar_nombre))
                    .addComponent(atras, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void atrasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_atrasActionPerformed
        atras();        // TODO add your handling code here:
    }//GEN-LAST:event_atrasActionPerformed

    private void agregar_clienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_agregar_clienteActionPerformed
        
        ConexionMySql con= new ConexionMySql();
        Connection reg= con.conectar();
        System.out.println("Conexion establecida");
        SimpleDateFormat fecha= new SimpleDateFormat("yyyy-MM-dd");
        Cliente clientes = new Cliente();
        
        //clientes.setIdcliente(Integer.parseInt(id_cliente.getText()));
        clientes.setCi(Integer.parseInt(ci_cliente.getText()));
        clientes.setNombre(nombre_cliente.getText());
        
        clientes.setDireccion(direccion_cliente.getText());
        clientes.setTelefono(telefono_cliente.getText());
        clientes.setCiudad(ciudad_cliente.getText());
        clientes.setFecha_ingreso(fecha.format(ingreso_cliente.getDate()) );
        String sql;
         sql="Insert into cliente (Ci,Nombre,Direccion,Telefono,Ciudad,Fecha_ingreso ) values "+"(?,?,?,?,?,?);";
        try {
            PreparedStatement pat=reg.prepareStatement(sql);
            //pat.setString(1, clientes.getIdcliente().toString());
            pat.setInt(1,clientes.getCi());
            pat.setString(2, clientes.getNombre());
            pat.setString(3,clientes.getDireccion());
            pat.setString(4,clientes.getTelefono());
            pat.setString(5,clientes.getCiudad());
            pat.setString(6,clientes.getFecha_ingreso());
            
            
            int var=pat.executeUpdate();
                if (var > 0){
                   JOptionPane.showMessageDialog(null, "Se Registro Con exito");
                   llamar();
                } 
            limpiar();
            
        } catch (SQLException ex) {
            Logger.getLogger(clientes.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
                // TODO add your handling code here:
    }//GEN-LAST:event_agregar_clienteActionPerformed

    private void eliminar_clienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eliminar_clienteActionPerformed
        ConexionMySql con = new ConexionMySql();
        Connection reg = con.conectar();
        System.out.println("Conexion establecida");
        Cliente clientes = new Cliente();
        clientes.setIdcliente(Integer.parseInt(id_cliente.getText()));

        String sql;
        sql = "DELETE FROM cliente WHERE id_cliente=?;";
        try {
            PreparedStatement pat = reg.prepareStatement(sql);
            pat.setInt(1, clientes.getIdcliente());
            pat.executeUpdate();

            JOptionPane.showMessageDialog(null, "Eliminacion Exitosa");
            llamar();
            limpiar();


        } catch (SQLException ex) {
            Logger.getLogger(clientes.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_eliminar_clienteActionPerformed

    private void actualizar_clienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_actualizar_clienteActionPerformed
        ConexionMySql con= new ConexionMySql();
        Connection reg= con.conectar();
        System.out.println("Conexion establecida");
        SimpleDateFormat fecha= new SimpleDateFormat("yyyy-MM-dd");
        Cliente clientes = new Cliente();
        clientes.setIdcliente(Integer.parseInt(id_cliente.getText()));
        clientes.setCi(Integer.parseInt(ci_cliente.getText()));
        clientes.setNombre(nombre_cliente.getText());
        clientes.setDireccion(direccion_cliente.getText());
        clientes.setTelefono(telefono_cliente.getText());
        clientes.setCiudad(ciudad_cliente.getText());
        clientes.setFecha_ingreso(fecha.format(ingreso_cliente.getDate()));
        
        
        
        String sql;
         sql="UPDATE cliente"+" SET Ci=?,"+" Nombre=?,"+" Direccion=?,"+" Telefono=?,"+" Ciudad=?,"+ "Fecha_ingreso=?"+" WHERE Id_Cliente=?;";
        try {
            PreparedStatement pat=reg.prepareStatement(sql);
            
            pat.setInt(1,clientes.getCi());
            pat.setString(2, clientes.getNombre());
            pat.setString(3,clientes.getDireccion());
            pat.setString(4,clientes.getTelefono());
            pat.setString(5,clientes.getCiudad());
            pat.setString(6,clientes.getFecha_ingreso());
            pat.setInt(7,clientes.getIdcliente());
            pat.executeUpdate();
            
            JOptionPane.showMessageDialog(null, "Modificacion Exitosa");
            
            llamar();
            limpiar();
            
        } catch (SQLException ex) {
            Logger.getLogger(clientes.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }//GEN-LAST:event_actualizar_clienteActionPerformed

    private void nuevo_clienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nuevo_clienteActionPerformed
       
        //id_cliente.setEnabled(true);
        ci_cliente.setEnabled(true);
        nombre_cliente.setEnabled(true);
        direccion_cliente.setEnabled(true);
        telefono_cliente.setEnabled(true);
        ciudad_cliente.setEnabled(true);
        ingreso_cliente.setEnabled(true);
       
        id_cliente.setText("");
        ci_cliente.setText("");
        nombre_cliente.setText("");
        direccion_cliente.setText("");
        telefono_cliente.setText("");
        ciudad_cliente.setText("");
        ingreso_cliente.setDate(null);
        
        agregar_cliente.setEnabled(true);
        eliminar_cliente.setEnabled(true);
        actualizar_cliente.setEnabled(true);
    }//GEN-LAST:event_nuevo_clienteActionPerformed

    private void nombre_clienteKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nombre_clienteKeyTyped
        char c = evt.getKeyChar();
        if(Character.isDigit(c)){
        getToolkit().beep();
        evt.consume();}
    }//GEN-LAST:event_nombre_clienteKeyTyped

    private void ci_clienteKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_ci_clienteKeyTyped
        char x = evt.getKeyChar();
        if((x<'0'||x>'9')) evt.consume();   
    }//GEN-LAST:event_ci_clienteKeyTyped

    private void direccion_clienteKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_direccion_clienteKeyTyped
           
    }//GEN-LAST:event_direccion_clienteKeyTyped

    private void telefono_clienteKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_telefono_clienteKeyTyped
        char x = evt.getKeyChar();
        if((x<'0'||x>'9')) evt.consume();       
    }//GEN-LAST:event_telefono_clienteKeyTyped

    private void ciudad_clienteKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_ciudad_clienteKeyTyped
        char c = evt.getKeyChar();
        if(Character.isDigit(c)){
        getToolkit().beep();
        evt.consume();}    
    }//GEN-LAST:event_ciudad_clienteKeyTyped

    private void id_clienteKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_id_clienteKeyTyped
        char x = evt.getKeyChar();
        if((x<'0'||x>'9')) evt.consume();
    }//GEN-LAST:event_id_clienteKeyTyped

    private void id_clienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_id_clienteActionPerformed
            
    }//GEN-LAST:event_id_clienteActionPerformed

    private void buscar_nombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscar_nombreActionPerformed
          
      ConexionMySql con = new ConexionMySql();
      Connection cnn = con.conectar();
      String[] titulos = {"Id Cliente","Doc Identidad","Nombre","Direccion","Telefono","Ciudad","Fecha de Ingreso"};
      busqueda = new DefaultTableModel(null,titulos);
      String [] registro = new String[8];
      String id = (cliente_busqueda.getText());
      String sql = "SELECT Id_Cliente,Ci,Nombre,Direccion,Telefono,Ciudad,Fecha_ingreso FROM cliente where Nombre LIKE '%" + id +"%'";
      try 
      {
        Statement st = cnn.createStatement();
        ResultSet rs = st.executeQuery(sql);
        
        while(rs.next()){
            registro[0]= rs.getString("id_Cliente");
            registro[1]= rs.getString("CI");
            registro[2]= rs.getString("Nombre");
            registro[3]= rs.getString("Direccion");
            registro[4]= rs.getString("Telefono");
            registro[5]= rs.getString("Ciudad");
            registro[6]= rs.getString("Fecha_ingreso");
            
            busqueda.addRow(registro);
        }
        vista.setModel(busqueda);
      } 
      catch (SQLException e) 
      {
      JOptionPane.showMessageDialog(null, e);
        
      }
    }//GEN-LAST:event_buscar_nombreActionPerformed

    private void vistaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_vistaMouseClicked
       int filasel = vista.getSelectedRow();
        String valor = (String) busqueda.getValueAt(filasel,0);
        PasarTabla(valor);   
    }//GEN-LAST:event_vistaMouseClicked

    private void direccion_clienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_direccion_clienteActionPerformed
            // TODO add your handling code here:
    }//GEN-LAST:event_direccion_clienteActionPerformed

    private void ciudad_clienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ciudad_clienteActionPerformed
         // TODO add your handling code here:
    }//GEN-LAST:event_ciudad_clienteActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(clientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(clientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(clientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(clientes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new clientes().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton actualizar_cliente;
    private javax.swing.JButton agregar_cliente;
    private javax.swing.JButton atras;
    private javax.swing.JButton buscar_nombre;
    private javax.swing.JTextField ci_cliente;
    private javax.swing.JTextField ciudad_cliente;
    private javax.swing.JTextField cliente_busqueda;
    private javax.swing.JTextField direccion_cliente;
    private javax.swing.JButton eliminar_cliente;
    private javax.swing.JTextField id_cliente;
    private com.toedter.calendar.JDateChooser ingreso_cliente;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField nombre_cliente;
    private javax.swing.JButton nuevo_cliente;
    private javax.swing.JTextField telefono_cliente;
    private javax.swing.JTable vista;
    // End of variables declaration//GEN-END:variables

    private void atras() {
        principal v=new principal();
        v.setVisible(true);
        v.setLocationRelativeTo(this);
        this.dispose();
    }

      

    String actualizar ="";
    private void PasarTabla(String valor) {
        ConexionMySql con = new ConexionMySql();
      Connection cnn = con.conectar();

      String sql = "SELECT Id_Cliente, Ci, Nombre, Direccion, Telefono, Ciudad, Fecha_ingreso FROM cliente WHERE Id_Cliente = "+ valor;
      try 
      {
        Statement st = cnn.createStatement();
        ResultSet rs = st.executeQuery(sql);
        if(rs.next()){
            id_cliente.setText(rs.getString(1)) ;
            ci_cliente.setText(rs.getString(2));
            nombre_cliente.setText(rs.getString(3));
            direccion_cliente.setText(rs.getString(4));
            telefono_cliente.setText(rs.getString(5));
            ciudad_cliente.setText(rs.getString(6));
            ingreso_cliente.setDate(rs.getDate(7));
          
        }
      } 
      catch (SQLException e) 
      {
            JOptionPane.showMessageDialog(null, e);
      }
      actualizar = valor;
    }
    private void llamar(){
      ConexionMySql con = new ConexionMySql();
      Connection cnn = con.conectar();
      String[] titulos = {"Id Cliente","Doc Identidad","Nombre","Direccion","Telefono","Ciudad","Fecha de Ingreso"};
      busqueda = new DefaultTableModel(null,titulos);
      String [] registro = new String[8];
      String id = (cliente_busqueda.getText());
      String sql = "SELECT Id_Cliente,Ci,Nombre,Direccion,Telefono,Ciudad,Fecha_ingreso FROM cliente where Nombre LIKE '%" + id +"%'";
      try 
      {
        Statement st = cnn.createStatement();
        ResultSet rs = st.executeQuery(sql);
        
        while(rs.next()){
            registro[0]= rs.getString("id_Cliente");
            registro[1]= rs.getString("CI");
            registro[2]= rs.getString("Nombre");
            registro[3]= rs.getString("Direccion");
            registro[4]= rs.getString("Telefono");
            registro[5]= rs.getString("Ciudad");
            registro[6]= rs.getString("Fecha_ingreso");
            
            busqueda.addRow(registro);
        }
        vista.setModel(busqueda);
      } 
      catch (SQLException e) 
      {
      JOptionPane.showMessageDialog(null, e);
        
      }
    }

    

    private void limpiar() {
        ci_cliente.setEnabled(true);
        nombre_cliente.setEnabled(true);
        direccion_cliente.setEnabled(true);
        telefono_cliente.setEnabled(true);
        ciudad_cliente.setEnabled(true);
        ingreso_cliente.setEnabled(true);
       
        id_cliente.setText("");
        ci_cliente.setText("");
        nombre_cliente.setText("");
        direccion_cliente.setText("");
        telefono_cliente.setText("");
        ciudad_cliente.setText("");
        ingreso_cliente.setDate(null);
        
        agregar_cliente.setEnabled(true);
        eliminar_cliente.setEnabled(true);
        actualizar_cliente.setEnabled(true);
    }
    
}
